import time

import cv2
import mss
import numpy as np

import scene
import action

area = {"left": 1080, "top": 308, "width": 418, "height": 780}

with mss.mss() as sct:
    while True:
        cut_main = sct.grab(area)
        cut_main = np.array(cut_main)
        # 转成灰度，更快查找
        cut_main_gray = cv2.cvtColor(cut_main, cv2.COLOR_BGR2GRAY)

        # 判断当前场景
        scene_now = scene.getscene(cut_main_gray)
        print('当前场景为: ', scene_now)
        # if scene_now != '未知':
        #     print('当前场景为: ', scene_now)

        if scene_now == '商店首页':
            action.click(cut_main_gray, "allgoods")
        elif scene_now == '全部商品':
            action.moveTo(260, 542)
            action.scroll(-2000)
            time.sleep(0.5)
        elif scene_now == '下个分类':
            action.click(cut_main_gray, "next")
            action.dragTo(260, 250, 0.5)
        elif scene_now == '到底了':
            print(1)

        # 调用opencv的imgshow方法显示图片
        cv2.imshow("tms", cut_main)
        # 等待按键关闭窗口，否则直接会退出
        # cv2.waitKey(0)
        # cv2.destroyAllWindows()
        # 等待5ms，用户输入q退出
        if cv2.waitKey(5) & 0xFF == ord("q"):
            cv2.destroyAllWindows()
            break
